/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Persistencia;

import DAO.AccessDatabase;
import Objetos.Meta;
import Objetos.Usuario;
import java.awt.HeadlessException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.JTable;

/**
 *
 * @author celiobj
 */
public class RepositorioMeta implements RepositorioMetaInterface{

    @Override
    public void adicionar(Meta meta, Usuario usu) {
    
         try {

            AccessDatabase a = new AccessDatabase();
            Connection con = a.conectar();
            Statement st = con.createStatement();
            st.execute("INSERT INTO `metas`(mes,ano,valor,codMes,qtdDiasUteis) VALUES('" + meta.getMes()+ "','" + meta.getAno() + "','" + meta.getValor()+ "','" + meta.getCodMes()+ "','" + meta.getQtdDiasUteis()+ "')");
            con.close();

            JOptionPane.showMessageDialog(null, "Meta cadastrada com sucesso!!!");
            Funcoes.funcoes.geraLog(" ", "Usuário: " + usu.getNome() + " | Adicionou a meta de " + meta.getMes()+"/"+meta.getAno()+ " às ");

        } catch (SQLException | HeadlessException t) {
            System.out.println(t.getMessage());

        }
        
    }

    @Override
    public void alterar(Meta antes, Meta depois, Usuario usu) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Meta procurarPeloMesAno(String mes, String ano, Usuario usu) {
        try {

            AccessDatabase a = new AccessDatabase();
            Connection con = a.conectar();
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM bd_oficinasimples.metas where codMes = '"+mes+"' and ano = '"+ano+"';");
            
            if(rs.next()){
            Meta me = new Meta();
            me.setMes(rs.getString("mes"));
            me.setAno(rs.getString("ano"));
            me.setCodMes("codMes");
            me.setValor(rs.getString("valor"));
            me.setQtdDiasUteis(rs.getString("qtdDiasUteis"));
            con.close();
            return me;
            }else{
                return null;
            }
        } catch (SQLException t) {
            System.out.println(t.getMessage());
            return null;

        }

    }

    @Override
    public JTable procurarTodasdoAno(String ano, Usuario usu) {
        try {
            Vector cabecalho = new Vector();
            Vector linhas = new Vector();
            AccessDatabase a = new AccessDatabase();
            JTable tabela;
            try (Connection con = a.conectar()) {
                Statement st = con.createStatement();
                ResultSet rs = st.executeQuery("SELECT mes,ano,valor,qtdDiasUteis FROM bd_oficinasimples.metas where ano = '"+ano+"' order by codMes;");
                rs.next();
                ResultSetMetaData rsmd = rs.getMetaData();
                cabecalho.addElement("Mês");
                cabecalho.addElement("Ano");
                cabecalho.addElement("Valor");
                cabecalho.addElement("Dias Úteis");
                
               
                do {
                    linhas.addElement(proximaLinha(rs, rsmd));
                } while (rs.next());
                tabela = new JTable(linhas, cabecalho);
            }
            tabela.getColumnModel().getColumn(0).setPreferredWidth(100);
            tabela.getColumnModel().getColumn(1).setPreferredWidth(100);
            tabela.getColumnModel().getColumn(2).setPreferredWidth(100);
            tabela.getColumnModel().getColumn(3).setPreferredWidth(80);
            
            
            
            return tabela;

        } catch (SQLException t) {
            System.out.println(t.getMessage());
            return null;

        }

    }
    
     private Vector proximaLinha(ResultSet rs, ResultSetMetaData rsmd) throws SQLException {
        Vector LinhaAtual = new Vector();

        try {
            for (int i = 1; i <= rsmd.getColumnCount(); ++i) {
                switch (rsmd.getColumnType(i)) {
                    case Types.VARCHAR:
                        LinhaAtual.addElement(rs.getString(i));
                        break;
                    case Types.TIMESTAMP:
                        LinhaAtual.addElement(rs.getDate(i).toLocaleString().substring(0, 10));
                        break;
                    case Types.INTEGER:
                        LinhaAtual.addElement(rs.getInt(i));
                        break;
                    case Types.DECIMAL:
                        LinhaAtual.addElement(Funcoes.funcoes.paraFormatoDinheiro(rs.getDouble(i)));
                        break;
                    case Types.DOUBLE:
                        LinhaAtual.addElement(Funcoes.funcoes.paraFormatoDinheiroRelatorio(rs.getDouble(i)).substring(3));
                        break;

                }
            }
        } catch (SQLException e) {
        }
        return LinhaAtual;

    }
    
}
